﻿using PLCTest.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;

namespace PLCTest.BO
{
    public class PlanarPuDoWriteWcsAction
    {
        public PlanarPuDoWriteWcsAction() { }
        public PlanarPuDoWriteWcsAction(
             int equipmentCode
            , short actionType
            , int toEquipmentCode
            , int taskCode
            , string palletBarcode
            , byte palletType
            , byte alrmCode
            , byte reserve = 0
            , byte reserve2 = 0
            , byte reserve3 = 0
            , byte reserve4 = 0)
        {
            EquipmentCode = equipmentCode;
            ActionType_1 = actionType;
            ToEquipmentCode_1 = toEquipmentCode;
            TaskCode_1 = taskCode;
            PalletBarcode_1 = palletBarcode;
            PalletType_1 = palletType;
            AlrmCode_1 = alrmCode;
            Reserve_1 = reserve;
            Reserve2_1 = reserve2;
            Reserve3_1 = reserve3;
            Reserve4_1 = reserve4;
        }

        /// <summary>
        /// 设备号
        /// </summary>
        public int EquipmentCode { get; set; }
        /// <summary>
        /// 任务号1
        /// </summary>
        public int TaskCode_1 { get; set; }
        /// <summary>
        /// 目标设备号1
        /// </summary>
        public int ToEquipmentCode_1 { get; set; }
        /// <summary>
        /// 托盘码1
        /// </summary>
        public string PalletBarcode_1 { get; set; }
        /// <summary>
        /// 握手类型1
        /// </summary>
        public short ActionType_1 { get; set; }
        /// <summary>
        /// 托盘类型1
        /// </summary>
        public byte PalletType_1 { get; set; }
        /// <summary>
        /// 报警代码1
        /// </summary>
        public byte AlrmCode_1 { get; set; }

        /// <summary>
        /// 单机任务标识1
        /// </summary>
        public byte Reserve_1 { get; set; }
        /// <summary>
        /// 预留2_1
        /// </summary>
        public byte Reserve2_1 { get; set; }
        /// <summary>
        /// 预留3_1
        /// </summary>
        public byte Reserve3_1 { get; set; }
        /// <summary>
        /// 预留4_2
        /// </summary>
        public byte Reserve4_1 { get; set; }



        /// <summary>
        /// 设备号2  无用 不写设备号
        /// </summary>
        public int EquipmentCode_2 { get; set; }
        /// <summary>
        /// 任务号2
        /// </summary>
        public int TaskCode_2 { get; set; }
        /// <summary>
        /// 目标设备号2
        /// </summary>
        public int ToEquipmentCode_2 { get; set; }
        /// <summary>
        /// 托盘码2
        /// </summary>
        public string PalletBarcode_2 { get; set; }
        /// <summary>
        /// 握手类型2
        /// </summary>
        public short ActionType_2 { get; set; }
        /// <summary>
        /// 托盘类型2
        /// </summary>
        public byte PalletType_2 { get; set; }
        /// <summary>
        /// 报警代码2
        /// </summary>
        public byte AlrmCode_2 { get; set; }

        /// <summary>
        /// 单机任务标识2
        /// </summary>
        public byte Reserve_2 { get; set; }
        /// <summary>
        /// 预留2_2
        /// </summary>
        public byte Reserve2_2 { get; set; }
        /// <summary>
        /// 预留3_2
        /// </summary>
        public byte Reserve3_2 { get; set; }
        /// <summary>
        /// 预留4_2
        /// </summary>
        public byte Reserve4_2 { get; set; }

        public byte[] Deserialize()
        {
            byte[] bytes = new byte[80];

            //工位1信息
            HelperByte.SetDIntAt(bytes, 0, EquipmentCode);
            HelperByte.SetDIntAt(bytes, 4, TaskCode_1);
            HelperByte.SetDIntAt(bytes, 8, ToEquipmentCode_1);

            HelperByte.SetByteAt(bytes, 12, 18);
            byte length = Convert.ToByte(PalletBarcode_1.Length);
            HelperByte.SetByteAt(bytes, 13, length);
            HelperByte.SetCharsAt(bytes, 14, PalletBarcode_1);

            HelperByte.SetIntAt(bytes, 32, ActionType_1);
            HelperByte.SetByteAt(bytes, 34, PalletType_1);
            HelperByte.SetByteAt(bytes, 35, AlrmCode_1);
            HelperByte.SetByteAt(bytes, 36, Reserve_1);
            HelperByte.SetByteAt(bytes, 37, Reserve2_1);
            HelperByte.SetByteAt(bytes, 38, Reserve3_1);
            HelperByte.SetByteAt(bytes, 39, Reserve4_1);

            //工位2信息
            HelperByte.SetDIntAt(bytes, 40, EquipmentCode_2);
            HelperByte.SetDIntAt(bytes, 44, TaskCode_2);
            HelperByte.SetDIntAt(bytes, 48, ToEquipmentCode_2);

            HelperByte.SetByteAt(bytes, 52, 18);
            byte length2 = Convert.ToByte(PalletBarcode_2.Length);
            HelperByte.SetByteAt(bytes, 53, length2);
            HelperByte.SetCharsAt(bytes, 54, PalletBarcode_2);

            HelperByte.SetIntAt(bytes, 72, ActionType_2);
            HelperByte.SetByteAt(bytes, 74, PalletType_2);
            HelperByte.SetByteAt(bytes, 75, AlrmCode_2);
            HelperByte.SetByteAt(bytes, 76, Reserve_2);
            HelperByte.SetByteAt(bytes, 77, Reserve2_2);
            HelperByte.SetByteAt(bytes, 78, Reserve3_2);
            HelperByte.SetByteAt(bytes, 79, Reserve4_2);

            return bytes;
        }

    }
}
